from datetime import datetime

# 连接influxdb
from influxdb import InfluxDBClient
from config import read_config
import pandas as pd

# 连接时序
def connect_influxdb():
    infux = read_config("influxdb")
    infux_client = InfluxDBClient(infux["host"], infux["port"], infux["username"], infux["password"], "remotedetection")  # 连接数据库
    return infux_client

# 读取和解析excel数据未json
def pareExcelData():
    influx_client = connect_influxdb()

    fr = open("data.csv", "r", encoding='utf-8')
    ls = []
    for line in fr:
        line = line.replace("\n", "")
        ls.append(line.split(","))
    fr.close()
    for i in range(1, len(ls)):
        ls[i] = dict(zip(ls[0], ls[i]))
    return ls[1:]


def toNs(tstr):
    time_str = tstr[12:]
    print(time_str)
    # 时间格式转化
    time_str = time_str[0:4] + '-' + time_str[4:6] + '-' + time_str[6:8] + ' ' + time_str[8:10] + ':' + time_str[10:12] + ':' + time_str[12:14]
    print(time_str)
    # 解析时间字符串为datetime对象
    dt_obj = datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S.%f")
    print(dt_obj)
    # 计算秒级时间戳并转换为纳秒
    timestamp_seconds = int(dt_obj.timestamp()) * 1000000000

    # 从原始字符串中提取微秒并转换为纳秒添加到总时间戳
    timestamp_nanoseconds = timestamp_seconds + dt_obj.microsecond * 1000

    return timestamp_nanoseconds

if __name__ == '__main__':
    toNs("B4452000020120240505163658")
    # list = pareExcelData()
    #
    # points = []
    #
    # for item in list:
    #     points.append({
    #     "measurement": "students",
    #     "time": toNs(item.),
    #     "tags": {
    #         "class": 1
    #     },
    #     "fields": {
    #         "name": "Hyc",
    #         "age": 3
    #     },
    # })






